package com.zjcode.cdcer.controller;

import com.chinatechstar.component.commons.controller.BaseController;
import com.chinatechstar.component.commons.supports.PageData;
import com.chinatechstar.component.commons.utils.ExcelUtils;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.service.CerSysteminitImportInfoService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@Slf4j
@RequestMapping("/v1/cerSysteminitImportInfo/")
public class CerSysteminitImportInfoController extends BaseController {

    @Autowired
    private CerSysteminitImportInfoService cerSysteminitImportInfoService;

    @PostMapping("listAllTableData")
    @ApiOperation("获取所有的初始数据信息列表")
    public JsonObj listByPage() {
        return cerSysteminitImportInfoService.listAllTableData(getPageData());
    }

    @PostMapping("batchDeletion")
    @ApiOperation("批量删除")
    public JsonObj batchDeletion() {
        PageData pageData = getPageData();
        return cerSysteminitImportInfoService.batchDeletion(pageData);
    }
    @PostMapping("addData")
    @ApiOperation("初始数据上报添加")
    public JsonObj addData(){
        PageData pageData = getPageData();
        return cerSysteminitImportInfoService.addData(pageData);
    }

    @GetMapping("oneClickDelete")
    @ApiOperation("一键删除")
    public JsonObj oneClickDelete() {
        PageData pageData = getPageData();
        return cerSysteminitImportInfoService.oneClickDelete(pageData);
    }

    @PostMapping("exportSysteminitImportInfo")
    @ApiOperation("导出初始数据")
    public void exportSysteminitImportInfo(HttpServletResponse response) {
        PageData pageData = getPageData();

        // 查询出可以用来导出的数据源
        JsonObj jsonObj = cerSysteminitImportInfoService.queryAllExportData(pageData);
        List<LinkedHashMap<String, Object>> dataList = (List<LinkedHashMap<String, Object>>) jsonObj
            .getData()
            .get("data");

        // 组装表头
        List<String> headList = Arrays.asList("姓名",
                                              "性别",
                                              "年龄",
                                              "身份证号",
                                              "地址",
                                              "联系电话",
                                              "门诊号",
                                              "住院号",
                                              "内镜诊疗日期",
                                              "对象来源",
                                              "内镜号",
                                              "是否无痛",
                                              "染色类别",
                                              "图像数量",
                                              "是否活检",
                                              "Hp检测结果",
                                              "Hp检测方法",
                                              "内镜诊断",
                                              "内镜诊断医师",
                                              "内镜审核医师",
                                              "病理号",
                                              "病理诊断",
                                              "食管病理诊断代码",
                                              "贲门病理诊断代码",
                                              "胃病理诊断代码",
                                              "十二指肠病理诊断代码",
                                              "病理诊断医师",
                                              "病理审核医师",
                                              "病理报告日期");

        try {
            // 导出
            ExcelUtils.exportExcel(headList, dataList, "初始数据导入", response);
        } catch (Exception e) {
        }
    }

    /**
     * 计划随访添加人员筛选
     */
    @PostMapping("/waitingFollowUpPersonnel")
    public JsonObj waitingFollowUpPersonnel() {
        return cerSysteminitImportInfoService.waitingFollowUpPersonnel(getPageData());
    }

    /**
     * 数据上报导出
     */
    @PostMapping("/newQueryAllExportData")
    public void newQueryAllExportData(HttpServletResponse response) {
        PageData pageData = getPageData();

        // 查询出可以用来导出的数据源
        JsonObj jsonObj = cerSysteminitImportInfoService.newQueryAllExportData(pageData);
        List<LinkedHashMap<String, Object>> dataList = (List<LinkedHashMap<String, Object>>) jsonObj
                .getData()
                .get("data");

        // 组装表头
        List<String> headList = Arrays.asList(
                "省份",
                "城市",
                "区县",
                "乡镇",
                "村",
                "机构名称",
                "调查对象ID号",
                "姓名",
                "性别",
                "生日",
                "民族",
                "身份证号",
                "地址",
                "联系电话",
                "筛查类型",
                "随访次数",
                "内镜号",
                "是否无痛",
                "染色类别",
                "是否活检",
                "内镜诊疗日期",
                "Hp",
                "Hp的检测方法",
                "图片数量",
                "内镜诊断",
                "内镜诊断医师",
                "内镜审核医师",
                "病理号",
                "病理报告日期",
                "食管病理诊断代码",
                "贲门病理诊断代码",
                "胃病理诊断代码",
                "十二指肠病理诊断代码",
                "病理诊断医师",
                "病理审核医师",
                "病理诊断");

        try {
            // 导出
            ExcelUtils.exportExcel(headList, dataList, "初始数据导出", response);
        } catch (Exception e) {
        }
    }


    /**
     * 删除数据上报
     */
    @GetMapping("/del")
    public JsonObj del() {
        PageData pageData = getPageData();
        String id = (String) pageData.get("id");
        return cerSysteminitImportInfoService.del(id);
    }

    /**
     * 图像上报
     */
    @PostMapping("/imageEscalation")
    public JsonObj imageEscalation() {
        return cerSysteminitImportInfoService.imageEscalation(getPageData());
    }

    /**
     * 体检表
     */
    @PostMapping("/editPhysicalExaminationInfo")
    public JsonObj editPhysicalExaminationInfo() {
        PageData pageData = getPageData();
        return cerSysteminitImportInfoService.editPhysicalExaminationInfo(pageData);
    }

    /**
     * 根据id查询体检表数据
     */
    @GetMapping("/getPhysicalExaminationByInfoId")
    public JsonObj getPhysicalExaminationByInfoId() {
        PageData pageData = getPageData();
        String infoId = String.valueOf(pageData.get("infoId"));
        return cerSysteminitImportInfoService.getPhysicalExaminationByInfoId(infoId);
    }

    @GetMapping("/getAreaByUserId")
    public JsonObj getAreaByUserId() {
        PageData pageData = getPageData();
        String userId = String.valueOf(pageData.get("userId"));
        return cerSysteminitImportInfoService.getAreaByUserId(userId);
    }
}
